<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@include file="/static/common/taglib.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <title>车次管理</title>
    <%@include file="../../public/commonCss.jsp" %>
    <%@include file="../../public/commonJs.jsp" %>
</head>
<body ms-controller="main">
<div id="wrapper">
    <div class="boxmain whiteBox lrbox">
        <div class="lrboxleft borderR" style="width:100%">
            <div class="from-header borderB contentbox">
                <div>
                    <span class="font14"> 所属分类:</span><span class="fontOrange2">车次</span>
                    <button class="layui-btn layui-btn-small layui-btn-normal" style="float:right;margin-left:20px"
                            ms-on-click="loadSearchData()">搜索
                    </button>
                    <input type="text" style="float:right;margin-left:20px" class="inputText" placeholder="搜索车次、始发站、终点站"
                           ms-duplex="searchKey.searchCode"/>
                </div>
            </div>
            <div class="from-body withheader contentbox" style=" overflow: auto;height:75%">
                <table class="layui-table">
                    <thead>
                    <tr align="center">
                        <th>行号</th>
                        <th>交路</th>
                        <th>车次</th>
                        <th>始发站</th>
                        <th>到达站</th>
                        <th>始发时间</th>
                        <th>到达时间</th>
                        <th>选择</th>
                    </tr>
                    </thead>
                    <tbody>
                    <tr align="center" ms-repeat="datas">
                        <td>{{$index+1}}</td>
                        <td>{{el.roadName}}</td>
                        <td>{{el.code}}</td>
                        <td>{{el.startCity}}</td>
                        <td>{{el.endCity}}</td>
                        <td>{{el.startTime}}</td>
                        <td>{{el.endTime}}</td>
                        <td>
                            <div class="tableText">
                                <button class="button" ms-on-click-1="$remove" ms-on-click="selectThis(el)">选择</button>
                            </div>
                        </td>
                    </tr>
                    </tbody>
                </table>

            </div>
            <div class="bottomButton moveArea" style="display:block;position:absolute;bottom:0;">
                <span style="float:left;margin-top:2px">已选择：<span
                        style="font-size:12px;color:blue">{{selectRoadStr}}</span></span>
                <button class="layui-btn layui-btn-normal layui-btn-small" ms-on-click="saveAll()">保存</button>
                <button class="layui-btn layui-btn-primary layui-btn-small" ms-on-click="closeLayer()">取消</button>
            </div>
        </div>
    </div>
    <!-- whitebox -->
</div>
<script type="text/javascript">
    var vm;
    var roadId = "${roadId}"
    $(function () {
        vm = avalon.define({
            $id: "main",
            datas: [],//表格数据
            currentPage: 1,
            selectRoads: [],
            selectRoadStr: "",
            pageSize: 10,
            searchKey: {
                searchCode: ""
            }
        });
        loadTableData(1);
    });
    //初始化分页插件的方法
    function loadTableData() {
        var index;
        var dataStr = $.param(vm.$model.searchKey);
        dataStr = dataStr + '&rid=' + roadId;
        $.ajax({
            type: 'post',
            url: '${ctx}/trainController/selectAllTrain.action',
            data: dataStr,
            dataType: "json",
            timeout: 10000,
            success: function (data) {
                vm.datas = data.datas;
            }
        });
    }
    //选择当前的车次
    function selectThis(el) {
        vm.selectRoads.push(el.id);
        vm.selectRoadStr = vm.selectRoadStr + el.code + ",";
    }
    //加载搜索的数据
    function loadSearchData() {
        loadTableData();
    }
    //保存所有
    function saveAll() {
        if (vm.$model.selectRoads.length == 0) {
            parent.layer.msg('你还没有选择任何车次!', {icon: 2});
            return;
        }
        var dataStr = {ids: vm.$model.selectRoads.toString(), roadId: roadId};
        $.ajax({
            type: 'post',
            url: '${ctx}/roadController/addTrainInRoad.action',
            data: dataStr,
            dataType: "json",
            timeout: 10000,
            success: function (data) {
                if (data.code == 0) {
                    closeLayer();
                    parent.layer.msg('保存成功!', {icon: 1});
                    parent.loadTableData();
                } else {
                    parent.layer.msg('保存失败!', {icon: 2});
                }
            }
        });
    }
    //关闭对话框
    function closeLayer() {
        parent.layer.closeAll();
    }
</script>
</body>
</html>